//
//  BTCustomTextView.swift
//  自定义UITextField
//
//  Created by lichangan on 16/1/31.
//  Copyright © 2016年 lichangan. All rights reserved.
//

import UIKit

 /// 当和导航控制器和UItabBarController一起使用的时候，将automaticallyAdjustsScrollViewInsets设为false
///否则将会出现文字不再顶部显示，而是显示距离顶部64的位置


class BTCustomTextView: UITextView {

    
    lazy var placeholder:UILabel = {
        
        let placeholder = UILabel()
        
        
        placeholder.text = "分享一点新鲜事..."
        placeholder.textColor = UIColor.lightGrayColor()
        placeholder.font = UIFont.systemFontOfSize(16)
        placeholder.sizeToFit()
        
        return placeholder
    }()
    
    
    init(frame:CGRect){
    
            super.init(frame:frame,textContainer: nil)
            self.font = UIFont.systemFontOfSize(16)
            self.delegate = self
        setupUI()
    }
    
   
    private func setupUI(){
        //当拖拽的时候收起已经弹起的键盘
        self.keyboardDismissMode = .OnDrag
        
        //设置垂直方向反弹效果
        self.alwaysBounceVertical = true

        addSubview(placeholder)
        
        placeholder.frame.origin = CGPoint(x: 5, y: 8)
        
    }
    
    
   
   required init?(coder aDecoder: NSCoder) {
       fatalError("init(coder:) has not been implemented")
   }
}

extension BTCustomTextView:UITextViewDelegate{
    
    func textViewDidChange(textView: UITextView){
    
        //隐藏placeholder
        placeholder.hidden = true
        
        //删除完所有输入内容的时候
        if textView.text.characters.count==0{
        
            //显示placeholder
            placeholder.hidden = false
        }
    }
    
}


